#include "rewine.h"

#include <cstdio>
#include <cstring>

#define ENDLINE "\\n\\\n"

using namespace std;

int main(int argc, char* argv[]) {
    rewine_AddFindPath("/as/code/play/dllcpt2/mylib");
    rewine_AddFindPath("/usr/local/lib64/wine");

    auto handleA = rewine_OpenImage("a.dll");
    auto handleB = rewine_OpenImage("b.dll");

    fprintf(stdout, "A.dll %p+0x%lx" ENDLINE, (void *)rewine_GetImageBase(handleA), rewine_GetImageSize(handleA));
    fprintf(stdout, "B.dll %p+0x%lx" ENDLINE, (void *)rewine_GetImageBase(handleB), rewine_GetImageSize(handleB));

    //auto paobj0 = (int *) rewine_GetExportByIndex(handleA, 6);
    //fprintf(stdout, "&aobj = %p" ENDLINE, paobj0);
    //fprintf(stdout, "aobj = %d" ENDLINE, *paobj0);

    auto paobj = (int *) rewine_GetExportByName(handleA, "aobj", -1);
    fprintf(stdout, "&aobj = %p" ENDLINE, paobj);
    fprintf(stdout, "aobj = %d" ENDLINE, *paobj);

    auto pbobj = (int *) rewine_GetExportByName(handleB, "bobj", -1);
    fprintf(stdout, "&bobj = %p" ENDLINE, pbobj);
    fprintf(stdout, "bobj = %d" ENDLINE, *pbobj);

    using bfunc_t = int (__cdecl *)(int, int);
    auto bfunc = (bfunc_t) rewine_GetExportByName(handleB, "bfunc", -1);
    fprintf(stdout, "&bfunc = %p" ENDLINE, (void *)bfunc);
    fprintf(stdout, "bfunc(1,2) = %d" ENDLINE, bfunc(1,2));

    using bfwd_t = int (__cdecl *)(int);
    auto bfwd = (bfwd_t) rewine_GetExportByName(handleB, "bfwd", -1);
    fprintf(stdout, "&bfwd = %p" ENDLINE, (void *)bfwd);
    fprintf(stdout, "bfwd(3) = %d" ENDLINE, bfwd(3));

    using breadfile_t = int (__cdecl *)(const char *);
    auto breadfile = (breadfile_t) rewine_GetExportByName(handleB, "readfile", -1);
    fprintf(stdout, "&readfile = %p" ENDLINE, (void *)bfwd);
    //breadfile("hello.txt");

    return 0;
}
